<?php
/**
 *	Role Views.
 *	@package		mv2.view.administration
 *	@extends		Core_View
 *	@uses			Framework_Krypton_View_Component_List
 *	@uses			Logic_Role
 *	@uses			Logic_Right
 *	@uses			Collection_Role
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	Role Views.
 *	@package		mv2.view.administration
 *	@extends		Core_View
 *	@uses			Framework_Krypton_View_Component_List
 *	@uses			Logic_Role
 *	@uses			Logic_Right
 *	@uses			Collection_Role
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
class View_Administration_Role extends Core_View
{
	/**
	 *	Builds Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$content	= "";
		if( $auth->isAuthenticated() )
		{
			if( $request->has( 'add' ) )
				$content	.= $this->buildContentAdd();
			else if( $request->has( 'roleId' ) )
			{
				$content	.= $this->buildContentEdit();
				$content	.= $this->buildContentPages();
			}
			else
				$content	.= $this->buildContentList();
		}
		return $content;
	}

	/**
	 *	Builds edit Role View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentAdd()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['role']['addRole'];

		$sources	= array( 'opt_location' => $opt_location );
		$ui			= $this->buildForm( 'administration/role', 'addRole', 'role', 'addRole', $values, $sources );
		$button_cancel	= $this->html->LinkButton( "?link=".$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_add		= $this->html->Button( 'addRole', $words['button_add'], 'but add' );
		$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
		$ui['field_button_add']		= $this->html->Field( '', $button_add);
		$ui['form']			= $this->html->Form( 'addRole', "?link=".$request->get( 'link' )."&add" );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'add' );
		$ui['heading']		= $words['heading'];

		return $this->loadTemplate( 'administration.role.add', $ui );
	}

	/**
	 *	Builds edit Role View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentEdit()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['role']['editRole'];
		$roleId	= $request->get( 'roleId' );

		$user	= new Model_Role( $roleId );
		$data	= $user->get();

		$ui	= $this->buildForm( 'administration/role', 'editRole', 'role', 'editRole', $data );
		$button_cancel	= $this->html->LinkButton( "?link=".$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_edit	= $this->html->Button( 'editRole', $words['button_edit'], 'but edit' );
		$button_remove	= $this->html->Button( 'removeRole', $words['button_remove'], 'buthot remove', $words['button_remove_confirm'] );
		$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
		$ui['field_buttons']		= $this->html->Field( '', $button_edit.$button_remove );
		$ui['form']			= $this->html->Form( 'editRole', "?link=".$request->get( 'link' )."&roleId=".$roleId );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['heading']		= $words['heading'];

		return $this->loadTemplate( 'administration.role.edit', $ui );
	}

	/**
	 *	Builds edit Role View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentList()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['role']['listRoles'];

		$options	= array(
			'coverage'		=> $config['layout.list_coverage'],
			'key_offset'	=> "filter_offset",
			);
		$heads	= array(
			$words['head_title'],
			$words['head_users'],
			);

		$actions	= array();
		$list	= new Framework_Krypton_View_Component_List( 'Collection_Role' );
		$list->setActionForm( 'listRoles' );
		$list->setActions( $actions );
		$list->setCaption( $words['caption'] );
		$list->setColumnGroup( $words['colgroup'] );
		$list->setTemplates( 'administration.role.list', 'administration.role.listitem', 'paging' );
		$list->setLimitKey( 'administration_roles_limit' );
		$list->setOffsetKey( 'administration_roles_offset' );
		$list->setHeads( $heads );
		$list->setPagingOptions( $options );
		$list->setTransformer( $this, 'transformListEntry' );

		$list->collectRoles();
		if( $session->get( 'administration_roles_order' ) )
			$list->orderBy( $session->get( 'administration_roles_order' ), $session->get( 'administration_roles_direction' ) );
		if( $session->get( 'administration_roles_title' ) )
			$list->withTitle( $session->get( 'administration_roles_title' ) );
		$list	= $list->getList( TRUE );

		$ui['list']		= $list;
		$ui['heading']	= $words['heading'];
		$ui['link_add']	= $this->html->LinkButton( "?link=".$request->get( 'link' )."&add", $words['link_add'], 'but add' );
		return $this->loadTemplate( 'administration.role.roles', $ui );
	}

	private function buildContentPages()
	{
		$request	= $this->registry->get( 'request' );
		$words		= $this->words['role']['setPages'];
		$pages		= $this->getPagesOfScope( "main" );

		$roleId		= $request->get( 'roleId' );
		$rolePages	= Logic_Role::getRolePages( $roleId );

		$i		= 0;
		$list	= array();
		foreach( $pages as $pageId => $pageData )
		{
			$parent	= isset( $pageData['parent'] ) ? $pageData['parent'] : "";
			$has	= in_array( $pageId, $rolePages );
			$scope	= "links_".$pageData['scope'];
			$item	= array();
			$roles	= array();
			foreach( $pageData['roles'] as $role )
			{
				$id	= Logic_Role::getRoleIdFromTitle( str_replace( "_", " ", $role ) );
				$roles[]	= $id ? $this->html->Link( "?link=roles&roleId=".$id, $role ) : $role;
			}
			$label			= isset( $this->words['main'][$scope][$pageId] ) ? strip_tags( $this->words['main'][$scope][$pageId] ) : $pageId;
			$item['roles']	= implode( ", ", $roles );
			$item['pages']	= isset( $pageData['pages'] ) ? $this->html->unorderedList( $pageData['pages'] ) : "";
			$item['check']	= $this->html->CheckBox( "links[]", $pageId, $has );
			$item['label']	= $parent ? $label : "<b>".$label."</b>";
			$item['indent']	= $parent ? "-&nbsp;&nbsp;" : "";
			$item['style']	= ++$i % 2 ? "list1" : "list2";
			$item	= $this->loadTemplate( 'administration.role.pages.listitem', $item );
			$list[]	= $item;
		}
		$ui['list']		= implode( $list );
		$button_edit	= $this->html->Button( 'setPages', $words['button_edit'], 'but edit' );
		$ui['field_button_edit']	= $this->html->Field( '', $button_edit );

		$heads	= array(
			'',
			$words['head_pages'],
			$words['head_roles']
		);
		$ui['heads']		= $this->html->TableHeads( $heads );
		$ui['form']			= $this->html->Form( 'setPages', "?link=".$request->get( 'link' )."&roleId=".$roleId );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['heading']		= $words['heading'];
		return $this->loadTemplate( 'administration.role.pages.list', $ui );
	}

	/**
	 *	Builds Extra Panel.
	 *	@access		public
	 *	@return		string
	 */
	public function buildExtra()
	{
//		return $this->loadContent( 'purchase.market.car.extra.html' );
	}

	protected function getPagesOfScope( $scope, $acceptedRoles = NULL )
	{
		$controller	= $this->registry->get( 'controller' );
		$pages		= $controller->getPages( $scope );

		$buffer		= array();
		$list		= array();
		foreach( $pages as $pageId => $pageData )
		{
			if( is_array( $acceptedRoles ) && !array_intersect( $pageData['roles'], $acceptedRoles ) )
				continue;
			$parent	= isset( $pageData['parent'] ) ? $pageData['parent'] : "";
			if( $parent )
			{
				$buffer[$parent][$pageId]	= $pageData;
				continue;
			}
			$list[$pageId]	= $pageData;
			if( isset( $buffer[$pageId] ) )
				foreach( $buffer[$pageId] as $pageId => $pageData )
					$list[$pageId]	= $pageData;
		}
		return $list;
	}

	public function transformListEntry( $entry )
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$words		= $this->words['role']['listRoles'];

		$item['link']		= $this->html->Link( "?link=".$request->get( 'link' )."&roleId=".$entry['roleId'], $entry['title'] );
		$list	= array();
		$users	= Logic_Role::getRoleUsers( $entry['roleId'] );
		foreach( $users as $userId => $userName )
			$list[]	= $this->html->Link( "?link=managers&userId=".$userId, $userName );
		$item['users']		= implode( ", ", $list );
		return $item;
	}
}
?>
